import { observable, action } from 'mobx'
import * as api from 'lib/constants/api'
import { post } from 'lib/net'
import Toast from 'components/toast'
import Load from 'components/load'

// class AddressModel {
//   constructor (data) {
//     if (data) {
//     }
//   }
// }

export default class BindWeChatVM {
  constructor (props) {
    this.props = props
    this.PageInitialization()
  }

  // 提现方式
  @observable cashType = ''

  // 获取code 后调转页面
  @action getCodeJump = async (code) => {
    let response = await post(api.WZBINDWX, { Code: code })
    if (response.Success) {
      window.sessionStorage.setItem('OpenId', response.Value)
      this.props.history.push('/wxMiddle')
    } else {
      Toast.fail(response.Message)
    }
  }
  // 微信授权
  @action handleWeChatJurisdiction = () => {
    window.sessionStorage.setItem('viewBox', 'false')
    window.location.href = 'https://yg.shundaonetwork.com/WX.html?appid=wx292f2a03126d7115&scope=snsapi_base&state=123&redirect_uri=' + encodeURIComponent(window.location.href)
  }

  @action PageInitialization = async () => {
    Load.loading(1, '加载中')
    const UserId = window.localStorage.getItem('UserId')
    let response = await post(api.WZUSERCASH, { WZId: UserId })
    if (response.Success) {
      Load.loading(2, '加载完成')
      this.cashType = response.Value.CashType
    } else {
      Toast.fail(response.Message)
    }
    let url = document.location.toString()
    let arrObj = url.split('?')
    if (arrObj.length > 1) {
      let arrPara = arrObj[1].split('&')
      let arr
      for (let i = 0; i < arrPara.length; i++) {
        arr = arrPara[i].split('=')
        if (arr != null && arr[0] === 'code') {
          this.getCodeJump(arr[1])
        }
      }
    }
  }
}
